#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
long long n,x,y,cnt=0,ans=0,dis[500005],siz[500005],d[500005],d2[500005];
vector<pair<long long,long long> > e1[500005],e2[500005],e;
vector<long long> edge[500005];
map<pair<long long,long long>,bool> mp;
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(long long i=0;i<(long long)edge[u].size();i++)
	{
		long long v=edge[u][i];
		if(v!=fa)
		{
			dfs(v,u);
			siz[u]+=siz[v];
			ans+=siz[v]*(n-siz[v]);
		}
	}
}
int main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(long long i=1;i<=n;i++)
	{
		cin>>x>>y;
		d[x]++,d2[y]++;
		e1[x].push_back({y,i});
		e2[y].push_back({x,i});
	}
	for(long long i=1;i<=n;i++)
	{
		d[i]+=d[i-1],d2[i]+=d2[i-1];
	}
	for(long long i=0;i<=500000;i++)
	{
		sort(e1[i].begin(),e1[i].end());
		sort(e2[i].begin(),e2[i].end());
		for(long long j=0;j<(long long)e1[i].size()-1;j++)
		{
			if(e1[i][j].first+1==e1[i][j+1].first)
			{
				if(!mp.count({e1[i][j].second,e1[i][j+1].second}))
				{
					edge[e1[i][j].second].push_back(e1[i][j+1].second);
					edge[e1[i][j+1].second].push_back(e1[i][j].second);
					mp[{e1[i][j].second,e1[i][j+1].second}]=1,mp[{e1[i][j+1].second,e1[i][j].second}]=1;
					e.push_back({e1[i][j].second,e1[i][j+1].second});			
				}
			}
		}
		for(long long j=0;j<(long long)e2[i].size()-1;j++)
		{
			if(e2[i][j].first+1==e2[i][j+1].first)
			{
				if(!mp.count({e2[i][j].second,e2[i][j+1].second}))
				{
					edge[e2[i][j].second].push_back(e2[i][j+1].second);
					edge[e2[i][j+1].second].push_back(e2[i][j].second);
					mp[{e2[i][j].second,e2[i][j+1].second}]=1,mp[{e2[i][j+1].second,e2[i][j].second}]=1;
					e.push_back({e2[i][j].second,e2[i][j+1].second});			
				}
			}
		}
	}
	if((long long)e.size()==n-1)
	{
		dfs(1,0);
		cout<<ans<<endl;
		return 0;
	}
	if(n<=4000)
	{
		for(long long i=1;i<=n;i++)
		{
			queue<long long> q;
			for(long long j=1;j<=n;j++)
			{
				dis[j]=1145141919810;
			}
			dis[i]=0;
			q.push(i);
			while(!q.empty())
			{
				long long u=q.front();
				q.pop();
				for(long long j=0;j<(long long)edge[u].size();j++)
				{
					long long v=edge[u][j];
					if(dis[v]>1e9)
					{
						dis[v]=dis[u]+1;
						q.push(v);
					}
				}
			}
			for(long long j=1;j<=n;j++)
			{
				ans+=dis[j];
			}
		}
		cout<<ans/2<<endl;
		return 0;
	}
	for(long long i=0;i<=500000;i++)
	{
		ans+=d[i]*(n-d[i])+d2[i]*(n-d2[i]);
	}
	cout<<ans<<endl;
	return 0;
}
